import * as crypto from 'crypto';
import { History } from './apis/entities/history.entity';

export const md5 = function (str) {
  const hash = crypto.createHash('md5');
  hash.update(str);
  return hash.digest('hex');
};

export const utcToBeijing = function (utcTime) {
  const beijingDate = new Date(utcTime);
  const year = beijingDate.getFullYear();
  const month = ('0' + (beijingDate.getMonth() + 1)).slice(-2); // 月份是从0开始的
  const day = ('0' + beijingDate.getDate()).slice(-2);
  const hours = ('0' + beijingDate.getHours()).slice(-2);
  const minutes = ('0' + beijingDate.getMinutes()).slice(-2);
  const seconds = ('0' + beijingDate.getSeconds()).slice(-2);

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

export class UnsignedInt {
  private _value: number;
  constructor(value: number) {
    if (value < 0) {
      throw new Error('Value must be non - negative');
    }
    this._value = value;
  }
  get value(): number {
    return this._value;
  }
  set value(newValue: number) {
    if (newValue < 0) {
      throw new Error('Value must be non - negative');
    }
    this._value = newValue;
  }
}

export const _BusinessTypeList = ['', 'blog', 'daily', 'comment'];

export const render = (
  res: History,
  picHtml: string,
  frontLink: string,
  nextLink: string,
  url: string,
) => {
  return `
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="renderer" content="webkit" />
    <meta name="viewport"
    content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" />
    <title>阿健|Finley的博客-${res.title}</title>
    <link rel="icon" href="https://jiangeges.oss-cn-heyuan.aliyuncs.com/front-end/icon.png" />
    <meta name="keywords" content="Vue3、前后端分离、个人博客网站">
    <meta name="description" content="前端使用vue3,typescript,vite后端使用nestjs加部分SSR开发的个人博客网站，用于记录前端知识学习分享及个人其他方面的记录">
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      .header {
        width: 100%;
        height: 60px;
        background-color: #ffffff;
        box-shadow: 0 3px 6px 3px rgba(7, 17, 27, 0.06);
      }
      ul {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
        display: flex;
        align-items: center;
        flex-direction: row;
      }
      li {
        list-style-type: none;
        // padding: 0 20px;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        font-size: 14px;
      }
      li:first-child {
        padding: 0;
      }
      li span {
        margin-left: 8px;
      }
      li:hover {
        background-color: #eef5fe;
      }
      h1 {
        padding: 0;
        margin: 12px 0;
        text-align: center
      }
      .his-main {
        box-shadow: 0 3px 6px 3px rgba(7, 17, 27, 0.06);
        padding: 12px
      }
      .his-main::after {
        content: "";
        clear: both
      }
      .bottom {
        display: flex;
        align-items: center;
        text-align: center;
      }
      .front-next-link {
        flex: 1;
        padding: 10px
      }
      @media screen and (min-width: 768px) {
        .his-main {
          max-width: 1080px;
          margin: 0 auto;
          min-height: calc(100vh - 180px)
        }
        .his-main img {
          width: 100%
        }
        ul {
          justify-content: flex-start;
        }
        li {
          padding: 0;
          margin-right: 20px;
        }
      }
      @media screen and (max-width: 768px) {
        .his-main {
          padding: 12px;
          margin: 0 auto;
          overflow-y: scroll;
          overflow-x: hidden;
        }
        .his-main img{
          width: 100%
        }
        ul {
          justify-content: space-between;
        }
        li {
          padding: 0;
          margin-right: 0px;
        }
      }
    </style>
  </head>

  <body>
  <div class="header">
    <ul>
      <li><img src="https://jiangeges.oss-cn-heyuan.aliyuncs.com/front-end/hqj.png" alt="前端加油站站长hqj的博客logo" width="100" height="60"></li>
      <li onclick="toMain('blog')"><svg t="1733672563265" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
          p-id="4235" width="14" height="14">
          <path
            d="M916.16 360.494L562.622 79.919C529.939 55.927 484.353 57 452.924 82.5L104.497 360.535c-19.798 16.064-31.225 39.73-31.225 64.668V919.3c0 21.173 17.164 38.337 38.337 38.337H320.31c21.173 0 38.337-17.164 38.337-38.337V682.871c0-34.874 29.614-63.407 65.809-63.407h175.289c36.195 0 65.809 28.533 65.809 63.407V919.3c0 21.173 17.164 38.337 38.337 38.337H912.39c21.173 0 38.337-17.164 38.337-38.337V427.742c0.001-26.385-12.784-51.258-34.567-67.248z"
            fill="" p-id="4236"></path>
        </svg><span>主页</span></li>
      <li onclick="toMain('ai')"><svg t="1734589298689" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4485" width="14" height="14"><path d="M554.666667 682.666667v42.666666h85.333333v85.333334H384v-85.333334h85.333333v-42.666666H170.666667V213.333333h682.666666v469.333334h-298.666666z m0-85.333334h213.333333V298.666667H256v298.666666h298.666667z" fill="#444444" p-id="4486"></path>
      </svg><span>AI</span></li>
      <li onclick="toMain('home')"><svg t="1733672629314" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
          p-id="5218" width="14" height="14">
          <path
            d="M0 64h1022.976v896H0V64z m80 809.536h863.488l-216.576-280.32-140.8 85.824-196.096-234.496-310.016 428.992z m617.984-463.296a85.12 85.12 0 1 0 0.064-170.368 85.12 85.12 0 0 0-0.064 170.368z"
            fill="#262626" p-id="5219"></path>
        </svg><span>相册</span></li>
      <li onclick="toMain('daily')"><svg t="1733673030855" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
          p-id="12687" width="14" height="14">
          <path
            d="M920.305313 126.35171 144.625912 126.35171c-33.751708 0-61.113893 27.361161-61.113893 61.113893l0 546.979676c0 33.751708 27.361161 61.113893 61.113893 61.113893l159.995971 0c4.067642 0 7.237845 3.524267 6.809079 7.569396l8.232498 114.813932c-0.423649 3.491521 1.757017 5.550413 4.244674 3.062755l129.002073-118.771057c4.273327-4.273327 2.90619-6.674003 8.948813-6.674003l458.446293 0c33.751708 0 61.113893-27.361161 61.113893-61.113893L981.419206 187.464579C981.419206 153.712871 954.058045 126.35171 920.305313 126.35171zM280.359292 534.87601c-44.843324 0-81.196277-36.352953-81.196277-81.196277 0-44.843324 36.352953-81.196277 81.196277-81.196277 44.843324 0 81.196277 36.352953 81.196277 81.196277C361.555569 498.523057 325.202617 534.87601 280.359292 534.87601zM540.279067 534.87601c-44.843324 0-81.196277-36.352953-81.196277-81.196277 0-44.843324 36.352953-81.196277 81.196277-81.196277 44.843324 0 81.196277 36.352953 81.196277 81.196277C621.475344 498.523057 585.122391 534.87601 540.279067 534.87601zM785.872554 534.87601c-44.843324 0-81.196277-36.352953-81.196277-81.196277 0-44.843324 36.352953-81.196277 81.196277-81.196277 44.843324 0 81.196277 36.352953 81.196277 81.196277C867.068831 498.523057 830.715879 534.87601 785.872554 534.87601z"
            p-id="12688"></path>
        </svg><span>说说</span></li>
    </ul>
  </div>
  <h1>${res.title}</h1>
  <div class="his-main">
    ${picHtml}
    <pre style="white-space: pre-wrap;font-size:18px;font-weight:500">${res.content}</pre>
    <div class="bottom">
      <div class="front-next-link">${frontLink}</div>
      <div class="front-next-link">${nextLink}</div>
    </div>
  </div>
  <script>
    function toMain(route) {
      window.open('${url}' + route, '_self')
    }
  </script>
  </body>
</html>`;
};

export const filterSensitiveWords = (text: string): string => {
  // 定义字典树节点构造函数
  function TrieNode() {
    this.children = {};
    this.isEndOfWord = false;
  }

  // 创建字典树的根节点
  const root = new TrieNode();

  // 将敏感词插入字典树
  function insert(word) {
    let node = root;
    for (const char of word) {
      if (!node.children[char]) {
        node.children[char] = new TrieNode();
      }
      node = node.children[char];
    }
    node.isEndOfWord = true;
  }

  // 初始化敏感词字典树，插入所有敏感词
  const politicsWords = [
    '政府',
    '习近',
    '共产',
    '毛泽东',
    '政变',
    '江青',
    '恐怖',
    '毒气',
    '袭击',
    '绑架',
    '杀人',
    '圣战',
    '国家',
  ];
  const dirtyWords = [
    '你爸',
    '你爹',
    '爸爸',
    '你妈',
    '臭逼',
    '臭嗨',
    '傻逼',
    '操逼',
    '操你',
    '妈逼',
    '个逼',
  ];
  const safeWords = [
    '<script',
    'script>',
    'evel',
    'alert',
    'window',
    'prompt',
    'location',
    'href',
    'iframe',
    'onload',
    'onerror',
  ];
  const sensitiveWords = [...politicsWords, ...dirtyWords, ...safeWords];
  sensitiveWords.forEach((word) => insert(word));

  let result = text;
  let i = 0;
  const n = text.length;
  while (i < n) {
    let node = root;
    let start = i;
    while (i < n && node.children[text[i]]) {
      node = node.children[text[i]];
      i++;
      if (node.isEndOfWord) {
        const wordLength = i - start;
        result =
          result.substring(0, start) +
          '*'.repeat(wordLength) +
          result.substring(i);
        i = start + wordLength;
      }
    }
    i++;
  }
  return result;
};
